var app = getApp();
//引入请求封装
import {
    request
} from "../../request/request.js"; //ES6 promise
// pages/myTask/myTask.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 静态图片存储路径
        img: app.globalData.image,
        //状态栏
        status: 2,
        // 底部提示
        load: false,
        load1: false,
        //任务信息
        pushTask: [],
        joinTask: [],
    },
    Data: {
        token: '',
        nowPage: 1, //当前在第几页
        nextNum: 3, //下一次希望传的任务数
    },
    // 切换状态栏
    changeStatus(e) {
        let id = e.currentTarget.dataset.id
        this.setData({
            status: id
        })
        this.Data.nowPage = 1
        this.getInfor()
    },
    // 点击完成任务
    complete() {
        wx.showModal({
            title: "是否确定所有参与者都已完成任务,确认后将会支付给所有参与者相应赏金",
            false() {
                return
            },
            success() {
                wx.showToast({
                    icon: 'none',
                    title: '已完成',
                })
            }
        })
    },
    // 获取已发布任务
    async getInfor() {
        let that = this
        if (this.data.status == 2) {
            const res = await request({
                url: "/myPushTask",
                data: this.Data,
                method: "get"
            })
            console.log(res)
            this.setData({
                pushTask: res.task
            })
        } else {
            const res = await request({
                url: "/myJoinTask",
                data: this.Data,
                method: "get"
            })
            console.log(res)
            this.setData({
                joinTask: res.task
            })
        }
    },
    // 追加请求发布任务
    async toPost() {
        let that = this
        this.Data.nowPage++;
        const res = await request({
            url: "/myPushTask",
            data: that.Data,
            method: "get"
        })
        console.log(res)
        if (res.tips == 'success') {
            let arr = that.data.pushTask
            let task = res.task
            task.forEach(i => {
                arr.push(i)
            });
            that.setData({
                pushTask: arr,
                load: false
            })
        } else if (res.tips = "数据已加载完") {
            that.setData({
                load: false,
                load1: true
            })
        }
    },
    async toPost2() {
        let that = this
        this.Data.nowPage++;
        const res = await request({
            url: "/myJoinTask",
            data: that.Data,
            method: "get"
        })
        console.log(res)
        if (res.tips == 'success') {
            let arr = that.data.joinTask
            let task = res.task
            task.forEach(i => {
                arr.push(i)
            });
            that.setData({
                joinTask: arr,
                load: false
            })
        } else if (res.tips = "数据已加载完") {
            that.setData({
                load: false,
                load1: true
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        wx.getStorage({
            key: 'token',
            success(res) {
                that.Data.token = res.data
                that.getInfor()
            },
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.setData({
            load: true
        })
        if (this.data.status == 2)
            this.toPost()
        else
            this.toPost2()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})